/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "foreground_app_connection_proxy_adapter.h"

#include "connection_observer_errors.h"
#include "hilog_tag_wrapper.h"
#include "string_ex.h"

namespace OHOS {
namespace AbilityRuntime {
namespace {
const std::u16string ABILITY_MGR_DESCRIPTOR = u"ohos.aafwk.AbilityManager";
constexpr uint32_t REGISTER_FOREGROUND_APP_CONNECTION_OBSERVER = 2507;
constexpr uint32_t UNREGISTER_FOREGROUND_APP_CONNECTION_OBSERVER = 2508;
}
int32_t ForegroundAppConnectionProxyAdapter::RegisterObserver(sptr<IForegroundAppConnection> observer)
{
    if (!observer) {
        TAG_LOGE(AAFwkTag::CONNECTION, "invalid IForegroundAppConnection");
        return ERR_INVALID_OBSERVER;
    }

    if (!remoteObj_) {
        TAG_LOGE(AAFwkTag::CONNECTION, "no abilityms proxy");
        return ERR_NO_PROXY;
    }

    int error;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
        TAG_LOGE(AAFwkTag::CONNECTION, "write token failed");
        return ERR_INVALID_VALUE;
    }

    if (!data.WriteRemoteObject(observer->AsObject())) {
        TAG_LOGE(AAFwkTag::CONNECTION, "write remote obj failed");
        return ERR_INVALID_VALUE;
    }

    error = remoteObj_->SendRequest(REGISTER_FOREGROUND_APP_CONNECTION_OBSERVER, data, reply, option);
    if (error != NO_ERROR) {
        TAG_LOGE(AAFwkTag::CONNECTION, "Send request error: %{public}d", error);
        return error;
    }
    return reply.ReadInt32();
}

int32_t ForegroundAppConnectionProxyAdapter::UnregisterObserver(sptr<IForegroundAppConnection> observer)
{
    if (!observer) {
        TAG_LOGE(AAFwkTag::CONNECTION, "IForegroundAppConnection invalid");
        return ERR_INVALID_OBSERVER;
    }

    if (!remoteObj_) {
        TAG_LOGE(AAFwkTag::CONNECTION, "no abilityms proxy");
        return ERR_NO_PROXY;
    }

    int error;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
        TAG_LOGE(AAFwkTag::CONNECTION, "write token failed");
        return ERR_INVALID_VALUE;
    }

    if (!data.WriteRemoteObject(observer->AsObject())) {
        TAG_LOGE(AAFwkTag::CONNECTION, "write remote obj failed");
        return ERR_INVALID_VALUE;
    }

    error = remoteObj_->SendRequest(UNREGISTER_FOREGROUND_APP_CONNECTION_OBSERVER, data, reply, option);
    if (error != NO_ERROR) {
        TAG_LOGE(AAFwkTag::CONNECTION, "Send request error: %{public}d", error);
        return error;
    }
    return reply.ReadInt32();
}

sptr<IRemoteObject> ForegroundAppConnectionProxyAdapter::GetProxyObject() const
{
    return remoteObj_;
}
}  // namespace AbilityRuntime
}  // namespace OHOS
